package com.quxuecx.springcloud.controller;

import com.quxuecx.springcloud.entities.CommonResult;
import com.quxuecx.springcloud.entities.Payment;
import com.quxuecx.springcloud.service.IPaymentService;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author: shaofeer
 * <p>
 * @qq: 337081267
 * <p>
 * @CSDN: http://blog.csdn.net/pyfysf
 * <p>
 * @blog: http://wintp.top
 * <p>
 * @email: shaofeer@163.com
 * <p>
 * @time: 2020/6/28
 */
@RestController
@RequestMapping("/service/payment")
public class PaymentController {
    @Resource
    private IPaymentService mIPaymentService;


    @Value("${server.port}")
    private String serverPort;

    /**
     * 保存
     *
     * @param payment
     * @return
     */
    @PostMapping("/save")
    public CommonResult save(@RequestBody Payment payment) {
        String serial = payment.getSerial();
        CommonResult commonResult = new CommonResult();

        if (serial == null || serial.isEmpty()) {
            commonResult.error();
            commonResult.setMessage(commonResult.getMessage() + "::当前服务器端口：" + serverPort);
            return commonResult;
        }

        int row = mIPaymentService.save(payment);
        commonResult.success(row);
        commonResult.setMessage(commonResult.getMessage() + "::当前服务器端口：" + serverPort);
        return commonResult;
    }

    /**
     * 根据id 获取
     *
     * @param id
     * @return
     */
    @GetMapping("/get/{id}")
    public CommonResult<Payment> getById(@PathVariable("id") Long id) {
        Payment payment = mIPaymentService.getById(id);
        CommonResult commonResult = new CommonResult();

        if (payment == null) {
            commonResult.error();
            commonResult.setMessage(commonResult.getMessage() + "::当前服务器端口：" + serverPort);

            return commonResult;
        }


        commonResult.success(payment);
        commonResult.setMessage(commonResult.getMessage() + "::当前服务器端口：" + serverPort);
        return commonResult;
    }
}
